package com.yk.filedownload.downloader;

import com.yk.filedownload.ConfigIt.Configuration;
import com.yk.filedownload.DataTransfer.KafkaOp.KafkaOperation;
import com.yk.filedownload.DataTransfer.KafkaOp.impl.KafkaOperationImpl;
import com.yk.filedownload.SimulationData;
import com.yk.filedownload.downloader.DownloadCell.impl.MuilThreadCell;
import com.yk.filedownload.downloader.DownloadCell.impl.SingleThreadCell;
import com.yk.filedownload.entity.HeaderCarrier;
import com.yk.filedownload.threadpool.DownloadThreadPool;
import com.yk.filedownload.util.FileType;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

/**
 * Project Name:cykfiledownload
 * File Name:DownloaderProducer
 * Package Name:com.yk.filedownload.service.muilThreadDownload
 * Date:2017\12\7 0007
 * Author:dengtianjia
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */
public class DownloaderProducer {

    private KafkaOperation kafkaOperation ;

    /**
     * 下载文件的线程池
     */
    public  DownloadThreadPool downloadThreadPool = null;

    public DownloaderProducer(DownloadThreadPool downloadThreadPool) throws Exception{
        this.downloadThreadPool = downloadThreadPool ;
        kafkaOperation = new KafkaOperationImpl();
    }

    public void start() {

        String url = null;
        if(SimulationData.simulatData.size() != 0 ) {
            url = SimulationData.simulatData.get(0);
        }
        try {
            if(url!=null) {

                String info = "" ;

                URL url_now = new URL(url);

                HttpURLConnection connection = (HttpURLConnection) url_now.openConnection();

                String length = connection.getHeaderField("Content-length");

                int code = connection.getResponseCode();

                if(code == 200){
                    int threadNum = (int)Math.ceil(Double.valueOf(length)/Configuration.MUIL_FILEDOWNLOAD_THRESHOLD);
                    //初始化文件头信息
                    HeaderCarrier headerCarrier = new HeaderCarrier();
                    String uuid = UUID.randomUUID().toString();
                    headerCarrier.setFileId(uuid);
                    headerCarrier.setFileSize(Integer.valueOf(length));
                    headerCarrier.setFileType(FileType.getExt(connection.getContentType(),url));
                    headerCarrier.setFileInfo(info);
                    headerCarrier.setPackNum(threadNum);
                    /**
                     * 将头信息传递至kafka
                     */
//                      kafkaOperation.getProducter().singleProduce(headerCarrier.getJson(),"");
                    SimulationData.simulaHeaderout(headerCarrier.getJson());
                    /**
                     * 判断url对应的下载方式
                     */
                    if(Integer.valueOf(length) <= Configuration.MUIL_FILEDOWNLOAD_THRESHOLD){
                        /**
                         * 单线程下载
                         */
                        Runnable singleCell = new SingleThreadCell(uuid,connection,Integer.valueOf(length));
                        this.downloadThreadPool.execute(singleCell);

                    }else{
                        /**
                         * 多线程下载
                         */
                        for(int i = 0 ; i < threadNum ; i ++){
                            String range;
                            int size;
                            if((Configuration.MUIL_FILEDOWNLOAD_THRESHOLD*(i+1)-1) <= Double.valueOf(length)) {
                                range = Configuration.MUIL_FILEDOWNLOAD_THRESHOLD * i + "-" + (Configuration.MUIL_FILEDOWNLOAD_THRESHOLD * (i + 1) - 1);
                                size = (Configuration.MUIL_FILEDOWNLOAD_THRESHOLD * (i + 1) - 1) - Configuration.MUIL_FILEDOWNLOAD_THRESHOLD * i + 1;
                            }else {
                                range = Configuration.MUIL_FILEDOWNLOAD_THRESHOLD * i + "-" + (Integer.valueOf(length)-1);
                                size = (Integer.valueOf(length)-1) - Configuration.MUIL_FILEDOWNLOAD_THRESHOLD * i;
                            }
                            Runnable muilCell = new MuilThreadCell(uuid+"@"+i,url,range,threadNum,size);
                            this.downloadThreadPool.execute(muilCell);
                        }
                    }

                }else if(code == 401){

                    // 放入身份认证错误连接队列
                }else{
                    // 放入下载受阻连接队列
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public DownloadThreadPool getDownloadThreadPool() {
        return downloadThreadPool;
    }

    public void setDownloadThreadPool(DownloadThreadPool downloadThreadPool) {
        this.downloadThreadPool = downloadThreadPool;
    }
}
